
<template>
    <div class="normal-container">
        <div>
            <div class="normal-text">{{ $route.name }}</div>
            <div class="content-main">
                <section>
                    <el-form :inline="true" :model="formInline" class="demo-form-inline">
                        <el-form-item label="角色名称">
                            <el-input v-model="formInline.name" placeholder="请输入角色名称" clearable></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" class="noraml-btn" @click="getList(true)">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" class="noraml-btn" @click="showDetail = true">新增</el-button>
                        </el-form-item>
                    </el-form>
                </section>
                <section>
                    <el-table v-loading="loading" ref="tableMain" :header-cell-style="{
                        'text-align': 'center',
                        background: '#F6F7FB',
                        color: '#4B4B4B',
                        fontSize: '13px',
                    }" :cell-style="{ 'text-align': 'center' }" :data="tableData" style="width: 100%"
                        :max-height="tableHeight">
                        <el-table-column type="index" label="序号" width="50"></el-table-column>
                        <el-table-column prop="username" label="用户名"></el-table-column>
                        <!-- <el-table-column prop="" label="密码"></el-table-column> -->
                        <el-table-column prop="name" label="姓名"></el-table-column>
                        <el-table-column prop="department" label="部门"></el-table-column>
                        <el-table-column prop="tel" label="电话"></el-table-column>
                        <el-table-column prop="remark" label="备注"></el-table-column>
                        <el-table-column fixed="right" label="操作">
                            <template slot-scope="scope">
                                <el-tooltip content="权限" placement="top-start" effect="light">
                                    <img @click="clickRole(scope.row)" style="margin-right: 10px;cursor: pointer;"
                                        src="@/assets/icon/icon-权限.png" alt="">
                                </el-tooltip>
                                <el-tooltip content="修改" placement="top-start" effect="light">
                                    <img @click="clickDetail(scope.row)" src="@/assets/icon/列表icon-修改.png"
                                        style="margin-right: 10px;cursor: pointer;" alt="">
                                </el-tooltip>
                                <el-tooltip content="删除" placement="top-start" effect="light">
                                    <img @click="clickDelete(scope.row)" src="@/assets/icon/列表icon-删除.png"
                                        style="margin-right: 10px;cursor: pointer;" alt="">
                                </el-tooltip>
                                <el-tooltip content="重置密码" placement="top-start" effect="light">
                                    <img @click="clickModify(scope.row.id)" src="@/assets/icon/重置密码.png"
                                        style="cursor: pointer;" alt="">
                                </el-tooltip>

                            </template>
                        </el-table-column>
                    </el-table>
                </section>
                <section style="text-align:center;padding-top:20px">
                    <el-pagination background @current-change="handleCurrentChange" :page-size="pageForm.size"
                        :current-page="pageForm.current" layout="prev, pager, next" :total="total">
                    </el-pagination>
                </section>
            </div>
        </div>
        <addItem v-if="showDetail" @closeDetail="closeDetail" :id="id" :showDetail="showDetail"></addItem>
        <role v-if="dialogVisible" :dialogVisible="dialogVisible" @closeDialog="closeDialog" :id="id" :pad="pad">
        </role>
    </div>
</template>
  
<script>
import axios from "axios";
import Cookies from 'js-cookie';
import addItem from './components/addDialog';
import role from './components/authority'
export default {
    components: { addItem, role },
    data() {

        return {
            pad: false,
            dialogVisible: false,
            messageDialog: false,

            showDetail: false,
            total: 0,
            loading: false,
            formInline: {
                name: ''
            },
            pageForm: {
                current: 1,
                size: 20
            },
            tableData: [],
            id: "",
            tableHeight: 500,
        }
    },
    mounted() {
        this.getList();
        this.$nextTick(res => {
            const tableHeight = document.documentElement.clientHeight;
            const topHeight = this.$refs.tableMain.$el.getBoundingClientRect().top + 115;
            this.tableHeight = tableHeight - topHeight;
        })
    },
    computed: {
        listenHeight() {
            return this.$store.state.user.heightSize
        }
    },
    watch: {
        listenHeight(val) {
            const tableHeight = document.documentElement.clientHeight;
            const topHeight = this.$refs.tableMain.$el.getBoundingClientRect().top + 115;
            this.tableHeight = tableHeight - topHeight;
        }
    },
    methods: {
        closeDialog(isFresh) {
            if (isFresh) this.getList();
            this.id = '';
            this.dialogVisible = false;
        },
        clickRole(item) {
            this.id = item.id;
            this.pad = item.pad;
            this.dialogVisible = true
        },
        clickModify(id) {
            let names = Cookies.get('tokenNameEqu');
            let value = Cookies.get('tokenValueEqu');
            let params = {
                'Content-Type': 'application/json; charset=utf-8'
            };
            params[names] = value;
            let that = this;
            axios({
                method: 'post',
                url: that.$http + '/openSafe',
                headers: params,
                data: JSON.stringify(value)
            }).then(function (response) {
                if (response.data.code == 500) {
                    that.$store.commit('user/SET_PASSORD', true);
                    that.$store.commit('user/SET_PASSORDID', id);
                } else {
                    that.$confirm('此操作将重置用户密码, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        that.$baseRequest('/sys/user/resetPwd/' + id, {}, {}, 'put').then(res => {
                            that.$message.success('重置密码成功');
                        })
                    })

                }
            })
        },
        save() {
            this.messageDialog = false;
        },
        clickDelete(item) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$baseRequest('/sys/user/delete/' + item.id, {}, {}, 'delete').then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList();
                })
            })
        },
        clickDetail(item) {
            this.id = item.id;
            this.showDetail = true;
        },
        closeDetail(isFresh) {
            if (isFresh) this.getList();
            this.showDetail = false;
            this.id = '';
        },
        getList(isClear) {
            this.loading = true;
            if (isClear) this.pageForm.current = 1; // 点击搜索
            this.$baseRequestGetParams("/sys/user/pageList", this.formInline, this.pageForm, "get").then(
                (res) => {
                    this.tableData = res.data.records;
                    this.total = Number(res.data.totalRow);
                    this.loading = false;
                }
            ).catch(res => {
                this.loading = false;
            })
        },
        handleCurrentChange(val) {
            this.pageForm.current = val;
            this.getList();
        },
    }
}
</script>
  
<style lang="scss" scoped>
.content-main {
    border: 1px solid #DFE2E8;
    padding: 30px;
    background-color: #fff;
}

.is-useing {
    color: #00B42A;
    position: relative;
}

.is-useing::before {
    content: '';
    position: absolute;
    left: 7px;
    top: 40%;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color: #00B42A;
}

.is-no-seing {
    color: #FF7D00;
    position: relative;
}

.is-no-seing::before {
    content: '';
    position: absolute;
    left: 7px;
    top: 40%;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color: #FF7D00;
}
</style>
  